package util;

import java.util.ArrayList;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;

/**
 * Trabalho PD - 2014/2015
 * @author a21210380 a21210392
 */
public final class Teste {
    
    private final ArrayList<User> users = new ArrayList<>();
    
    public Teste() {
        users.clear();
        addUser("João", 34, "address teste 1");
        addUser("Pedro", 34, "address  2 teste");
        addUser("David", 34, " teste address 3");
        addUser("Luís", 34, " teste 4address");
        addUser("André", 34, " 5 ds address teste 5 spain");
    }
    
     
    
    public boolean addUser(String name, int age, String address) {
        if(users.isEmpty()) {
            users.add(new User(0, name, age, address));
            return true;
        } else {
            int id = users.get(users.size()-1).getId() + 1;
            users.add(new User(id, name, age, address));
            return true;
        }
    }
    
    public boolean removeUser(int id) {
        for(int i=0; i<users.size(); ++i) {
            if(users.get(i).getId() == id) {
                users.remove(i);
                return true;
            }
        }
        return false;
    }
    
    public boolean editUser(int id, String name, int age, String address) {
        for(int i=0; i<users.size(); ++i) {
            User get = users.get(i);
            if(get.getId() == id) {
                get.setName(name);
                get.setAge(age);
                get.setAddress(address);
                return true;
            }
        }
        return false;
    }
    
    public JsonArray toJson() {
        JsonArrayBuilder createArrayBuilder = Json.createArrayBuilder();
        for(User u : users) {
            createArrayBuilder.add(u.toJson());
        }
        return createArrayBuilder.build();
    }
    
    private class User {
        private final int id;
        private String name;
        private int age;
        private String address;

        public User(int id, String name, int age, String address) {
            this.id = id;
            this.name = name;
            this.age = age;
            this.address = address;
        }

        public int getId() {
            return id;
        }
        
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }
        
        public JsonObject toJson() {
            return Json.createObjectBuilder()
                    .add("id", id)
                    .add("name", name)
                    .add("age", age)
                    .add("address", address)
                    .build();
        }
    }
    
}
